package com.puppet.building182.service.impl;

import com.puppet.building182.handler.MyAssert;
import com.puppet.building182.mapper.PermissionMapper;
import com.puppet.building182.pojo.Permission;
import com.puppet.building182.pojo.User;
import com.puppet.building182.pojo.VO.PermissionVO;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.PermissionService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public HashMap<String, List> getPermission(Integer id) {
        //1.查询所有的一级权限
        List<PermissionVO> firstPerms = permissionMapper.getAllPerms(0);
        //1.2 用现在的一级权限遍历， 搜索这些一级权限的二级权限
        firstPerms =getPerms(firstPerms);//用递归方法

//        for (PermissionVO first : firstPerms) {
//            List<PermissionVO> second = permissionMapper.getAllPerms(first.getId());
//            //1.3 将当前二级权限集合赋值给一级的children属性
//
//            first.setChildren(second);
//            for (PermissionVO s : second) {
//                List<PermissionVO> third = permissionMapper.getAllPerms(s.getId());
//                s.setChildren(third);
//            }
//        }
//        查询出当前选中角色的权限信息
        List<Integer> allPermsByRid = permissionMapper.getAllPermsByRid(id);
        List<Integer> pids = new ArrayList<>();
        for (Integer pid : allPermsByRid) {
            Integer parenId = permissionMapper.getParentId(pid);
            pids.add(parenId);
        }
        allPermsByRid.removeAll(pids);


        HashMap<String, List> map = new HashMap<>();
        map.put("all", firstPerms);
        map.put("permsByRid", allPermsByRid);
        return map;
    }
private List<PermissionVO> getPerms(List<PermissionVO> first) {
        for (PermissionVO permissionVO : first) {
            List<PermissionVO> second = permissionMapper.getAllPerms(permissionVO.getId());
            permissionVO.setChildren(second);
            if(!second.isEmpty()){
                getPerms(second);
            }

        }
        return first;
}
    @Override
    public Integer savePermission(Integer id, List<Integer> ids) {
        Integer num=permissionMapper.deletePerByRid(id);
        if(!ids.isEmpty()){
            num=permissionMapper.saveNewPerms(id,ids);
        }
        MyAssert.error(num<=0, BuildingCode.SAVE_FAIL.getMessage());
        return num;
    }


//    正式页面

    @Override
    public List<Permission> getAllPermission() {
        return permissionMapper.getAllPermission();
    }

    @Override
    public Integer deletePerms(Integer id) {
        return permissionMapper.deletePerms(id);

    }

    @Override
    public Permission getById(Integer id) {
        return permissionMapper.getById(id);
    }

    @Override
    public Integer updatePermission(Permission permissions) {
        return permissionMapper.updatePermission(permissions);

    }

    @Override
    public Integer addPermsByFile(List<Permission> data) {
        MyAssert.error( (data.isEmpty()), BuildingCode.SAVE_FAIL.getMessage());
        return permissionMapper.addPermsByFile(data);
    }

    @Override
    public List<Permission> getAllPermission(Integer userId) {
    List<Permission> first=permissionMapper.getAllMenuPerms(userId,0);
    for(Permission permission:first){
    List<Permission> second=permissionMapper.getAllMenuPerms(userId,permission.getId());
    permission.setChildren(second);
    }
    return first;
    }

    @Override
    public List<String> getAllButtonPerms() {
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        User user=(User)request.getSession().getAttribute("user");
        return permissionMapper.getAllButtonPerms(user.getId());
    }
}
